
#[derive(Debug)]
pub struct PriceTick {
    pub time: u32,
    pub open: f64,
    pub high: f64,
    pub low: f64,
    pub close: f64,
    pub volume: u32,
}
/// The euclidean_distance_kline function now takes two slices of PriceTick structs (base and kline) as input parameters.
/// The function calculates the Euclidean distance between the corresponding close prices and volume values for each pair of PriceTick structs in the two sequences.
pub fn euclidean_distance_kline(base: &[PriceTick], kline: &[PriceTick]) -> f64 {
    let squared_diff_sum: f64 = base
        .iter()
        .zip(kline)
        .map(|(base_tick, kline_tick)| {
            let price_diff = base_tick.close - kline_tick.close;
            let volume_diff = base_tick.volume as f64 - kline_tick.volume as f64;
            (price_diff * price_diff) + (volume_diff * volume_diff)
        })
        .sum();

    squared_diff_sum.sqrt()
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]

    /// The main function demonstrates how to use the euclidean_distance_kline function to calculate the Euclidean distance between two sets of K-lines.
    fn test_main() {
        // Example usage
        let base_kline = vec![
            PriceTick { time: 1, open: 98.0, high: 105.0, low: 97.0, close: 102.0, volume: 100, },
            PriceTick { time: 2, open: 102.0, high: 108.0, low: 100.0, close: 105.0, volume: 120, },
            PriceTick { time: 2, open: 102.0, high: 108.0, low: 100.0, close: 105.0, volume: 120, },
            PriceTick { time: 2, open: 102.0, high: 108.0, low: 100.0, close: 105.0, volume: 120, },
        ];

        let kline1_to_compare = vec![
            PriceTick { time: 1, open: 95.0, high: 100.0, low: 92.0, close: 98.0, volume: 95, },
            PriceTick { time: 2, open: 100.0, high: 105.0, low: 98.0, close: 102.0, volume: 110, },
            PriceTick { time: 2, open: 100.0, high: 105.0, low: 98.0, close: 103.0, volume: 120, },
            PriceTick { time: 2, open: 100.0, high: 105.0, low: 98.0, close: 112.0, volume: 160, },
        ];

        let kline2_to_compare = vec![
            PriceTick { time: 1, open: 95.0, high: 100.0, low: 92.0, close: 98.0, volume: 95, },
            PriceTick { time: 2, open: 100.0, high: 105.0, low: 98.0, close: 102.0, volume: 110, },
            PriceTick { time: 2, open: 100.0, high: 105.0, low: 98.0, close: 103.0, volume: 120, },
            PriceTick { time: 2, open: 100.0, high: 105.0, low: 98.0, close: 102.0, volume: 130, },
        ];

        let distance1 = euclidean_distance_kline(&base_kline, &kline1_to_compare);
        let distance2 = euclidean_distance_kline(&base_kline, &kline2_to_compare);
        println!("Euclidean distance between two K-lines: distance1={} distance2={}", distance1,distance2);
    }
}
